/**
 * @Project TMHSCFX
 * @Author Tran Manh Hung
 * @Email: hungtmit@gmail.com | tmh@xwebgate.com
 * @Copyright © 2011 TMH. All rights reserved
 * @Createdate Feb 28, 2011
 * @Createtime 5:11:13 PM
 */

package com.goToAndLearn.aS3IsoLib
{
	import as3isolib.display.IsoSprite;
	import as3isolib.display.IsoView;
	import as3isolib.display.primitive.IsoBox;
	import as3isolib.display.scene.IsoGrid;
	import as3isolib.display.scene.IsoScene;
	import as3isolib.geom.IsoMath;
	import as3isolib.geom.Pt;
	import as3isolib.graphics.Stroke;

	import com.greensock.TweenMax;

	import eDpLib.events.ProxyEvent;

	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;

	import mx.effects.Tween;

	public class Iso extends Sprite
	{
		private static const CELL_SIZE:Number = 50;

		private var grid:IsoGrid;

		private var scene:IsoScene;

		private var view:IsoView;

		private var box:IsoBox;

		public function Iso()
		{
			super();

			scene = new IsoScene();

			grid = new IsoGrid();
			//grid.addEventListener(MouseEvent.MOUSE_MOVE, grid_mouseMoveHandler);
			grid.showOrigin = false;
			grid.setGridSize(8, 8, 0);
			grid.gridlines = new Stroke(1, 0x000000);
			grid.cellSize = CELL_SIZE;

			var iso:IsoSprite;

			for (var i:int = 0; i < 8; i++)
			{
				for (var j:int = 0; j < 8; j++)
				{
					iso = new IsoSprite();
					iso.addEventListener(MouseEvent.MOUSE_OVER, iso_mouseOverHandler);
					iso.addEventListener(MouseEvent.MOUSE_OUT, iso_mouseOutHandler);
					iso.addEventListener(MouseEvent.CLICK, iso_clickHandler);

					if (Math.random() > .5)
					{
						iso.sprites = [brick];
					}
					else
					{
						iso.sprites = [grass];
					}

					iso.moveBy(j * 50, i * 50, 0);

					scene.addChild(iso);
				}
			}

			box = new IsoBox();
			box.setSize(CELL_SIZE, CELL_SIZE, 5);

			//scene.addChild(grid);
			//scene.addChild(box);

			view = new IsoView();
			view.centerOnPt(new Pt(200, 200));
			view.setSize(800, 500);
			view.addScene(scene);

			addChild(view);

			addEventListener(Event.ENTER_FRAME, loop);
		}

		private function iso_clickHandler(event:ProxyEvent):void
		{
			var iso:IsoSprite = event.proxy as IsoSprite;

			iso.sprites = [brick];
			scene.render();
		}

		private function iso_mouseOutHandler(event:ProxyEvent):void
		{
			var iso:IsoSprite = event.proxy as IsoSprite;
			TweenMax.to(iso.container, .5, {alpha:1});
		}

		private function loop(event:Event):void
		{
			scene.render();
		}

		private function iso_mouseOverHandler(event:ProxyEvent):void
		{
			var iso:IsoSprite = event.proxy as IsoSprite;
			TweenMax.to(iso.container, .5, {alpha:.3});
			//TweenMax.to(iso.container, .5, {alpha:.5, yoyo:true, repeat:1, startAt:{alpha:1}});
		}

		private function grid_mouseMoveHandler(event:ProxyEvent):void
		{
			var me:MouseEvent = MouseEvent(event.targetEvent);
			var pt:Pt = new Pt(me.localX, me.localY);
			IsoMath.screenToIso(pt);
			box.moveTo(Math.floor(pt.x / CELL_SIZE) * CELL_SIZE, Math.floor(pt.y / CELL_SIZE) * CELL_SIZE, 0);
			scene.render();
		}
	}
}